package com.zhoukun.cloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhoukun.cloud.CommentRest;
import com.zhoukun.cloud.pojo.tAccount;
import com.zhoukun.cloud.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author zhoukun 86547462@qq.com
 * @version 1.0
 * @date 2020/11/2 22:54
 */
@RestController
public class accountController {

    @Autowired
    AccountService service;
    @GetMapping("/account/getall")
    public CommentRest getall()
    {
        return new CommentRest().setCode(200).setData(service.list());
    }

    //扣钱操作
    @GetMapping("/account/up/{userid}/{money}")
    public CommentRest up(@PathVariable Long userid,@PathVariable BigDecimal money)
    {
        tAccount account=service.getById(userid);
        if(account.getResidue().compareTo(money) < 0)
        {
            return new CommentRest().setCode(400).setMessage("您的额余额不足！");
        }
        QueryWrapper<tAccount> wrapper=new QueryWrapper<tAccount>();
        wrapper.eq("user_id",userid);
        service.update(account.setResidue(account.getResidue().subtract(money))
                .setUsed(account.getUsed().add(money)),wrapper);
        return new CommentRest().setCode(200).setMessage("success");
    }
}
